package com.zhongjh.app.phone.productdetail

import android.graphics.Color
import android.os.Bundle
import androidx.activity.viewModels
import androidx.annotation.VisibleForTesting
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.github.razir.progressbutton.attachTextChangeAnimator
import com.github.razir.progressbutton.bindProgressButton
import com.github.razir.progressbutton.hideProgress
import com.github.razir.progressbutton.showProgress
import com.gyf.immersionbar.ImmersionBar
import com.zhongjh.app.R
import com.zhongjh.app.databinding.ActivityProductDetailBinding
import com.zhongjh.app.entity.ProductDetailType
import com.zhongjh.app.phone.main.fragment.shopping.ShopPingFragment
import com.zhongjh.app.phone.shoppingcart.ShoppingCartViewManager
import com.zhongjh.app.phone.shoppingcart.shoppingCartCountFlow
import com.zhongjh.mvvmibatis.base.ui.BaseActivity
import com.zhongjh.mvvmibatis.entity.State
import dagger.hilt.android.AndroidEntryPoint
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator

/**
 * 产品详情
 * @author zhongjh
 * @date 2022/7/8
 */
@AndroidEntryPoint
class ProductDetailActivity :
    BaseActivity<ActivityProductDetailBinding>(R.layout.activity_product_detail) {

    @get:VisibleForTesting
    internal val viewModel: ProductDetailViewModel by viewModels()

    private lateinit var mCommonNavigatorAdapter: ProductDetailNavigatorAdapter
    private val mShoppingCartViewManager: ShoppingCartViewManager by lazy {
        ShoppingCartViewManager(mBinding.layoutSearch.tvShoppingCartCount, 0)
    }

    override fun initParam(savedInstanceState: Bundle?) {
    }

    override fun initialize() {
        initMagicIndicator()
        bindProgressButton(mBinding.btnAddShoppingCart)
        mBinding.btnAddShoppingCart.attachTextChangeAnimator()
    }

    override fun initListener() {
        mCommonNavigatorAdapter.setOnItemListener(object :
            ProductDetailNavigatorAdapter.OnClickListener {
            override fun onItemListener(index: Int) {
                when (index) {
                    0 -> {
                        mBinding.nestedScrollView.smoothScrollTo(0, mBinding.vBlack.top)
                    }
                    1 -> {
                        mBinding.nestedScrollView.smoothScrollTo(0, mBinding.vLilac.top)
                    }
                    2 -> {
                        mBinding.nestedScrollView.smoothScrollTo(0, mBinding.vLightRed.top)
                    }
                }
            }
        })
        mBinding.nestedScrollView.setOnScrollChangeListener(
            NestedScrollView.OnScrollChangeListener { _, _, scrollY, _, _ ->
                val height = mBinding.magicIndicator.height * 10
                when (scrollY) {
                    0 -> {
                        mBinding.magicIndicator.alpha = 0.0f
                    }
                    in 1..height -> {
                        val scale: Float = scrollY.toFloat() / height
                        mBinding.magicIndicator.alpha = scale
                    }
                    else -> {
                        mBinding.magicIndicator.alpha = 1.0f
                    }
                }

                // 判断某个控件是否滚到顶部
                if (scrollY >= mBinding.vBlack.top && scrollY < mBinding.vLilac.top) {
                    mBinding.magicIndicator.onPageScrolled(
                        0,
                        0F,
                        0
                    )
                } else if (scrollY >= mBinding.vLilac.top && scrollY < mBinding.vLightRed.top) {
                    mBinding.magicIndicator.onPageScrolled(
                        1,
                        0F,
                        0
                    )
                } else if (scrollY >= mBinding.vLightRed.top) {
                    mBinding.magicIndicator.onPageScrolled(
                        2,
                        0F,
                        0
                    )
                }
            }
        )
        mBinding.btnAddShoppingCart.setOnClickListener {
            viewModel.shoppingCartAdd()
        }
    }

    override fun initObserver() {
        lifecycleScope.launchWhenStarted {
            shoppingCartCountFlow.collect {
                when (it) {
                    is State.Loading -> {
                        mBinding.btnAddShoppingCart.showProgress {
                            progressColor = Color.WHITE
                        }
                    }
                    is State.Success -> {
                        mShoppingCartViewManager.amount = it.data
                        mBinding.btnAddShoppingCart.hideProgress(R.string.add_shopping_cart)
                    }
                    else -> {}
                }
            }
        }
    }

    override fun initImmersionBar() {
        ImmersionBar
            .with(this)
            .titleBarMarginTop(mBinding.layoutSearch.vTop)
            .statusBarColorTransformEnable(false)
            .navigationBarColor(R.color.aurora_grey)
            .statusBarDarkFont(true)
            .navigationBarDarkIcon(true)
            .init()
    }

    /**
     * 初始化tab
     */
    private fun initMagicIndicator() {
        val listTitle: MutableList<String> = ArrayList()
        val listFragments: MutableList<Fragment> = ArrayList()
        // 实例化所有枚举信息
        val productDetailType = ProductDetailType.values()
        for (searchType in productDetailType) {
            listTitle.add(searchType.value)
            listFragments.add(ShopPingFragment())
        }

        val commonNavigator = CommonNavigator(this)
        mCommonNavigatorAdapter = ProductDetailNavigatorAdapter(listTitle)
        commonNavigator.adapter = mCommonNavigatorAdapter
        commonNavigator.leftPadding = UIUtil.dip2px(this, 10.0)
        commonNavigator.rightPadding = UIUtil.dip2px(this, 15.0)
        mBinding.magicIndicator.navigator = commonNavigator
    }
}